﻿using Nemerle.Collections;
using Nemerle.Text;
using Nemerle.Utility;

using System;
using SCG = System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Xml.XPath;
using System.IO;
using System.Configuration;

namespace NRails
{
  public class Configuration
  {
      static defaultPrefix = "NRails" : string;
      [Accessor] mutable env : string;
      connectionNames = SCG.Dictionary.[string, string]() : SCG.Dictionary[string, string];
      [Accessor] connectionStrings : SCG.Dictionary[string, ConnectionStringSettings] = SCG.Dictionary.[string, ConnectionStringSettings]();
      
      public this(env : string, connectionStrings : SCG.Dictionary[string, ConnectionStringSettings])
      {
        this.env = env;
        this.connectionStrings = connectionStrings;
      }
      
      public GetConnectionStringName(env : string) : string
      {
          if (connectionNames.ContainsKey(env))
              connectionNames[env];
          else
          {
              def default = $"$defaultPrefix.$env";
              connectionNames[env] = default;
              default;
          }
      }

      public GetConnectionString(env : string) : ConnectionStringSettings
      {
          def name = GetConnectionStringName(env);
          connectionStrings[name];
      }

      public ConnectionStringName : string
      {
          get {GetConnectionStringName(env);}
      }

      public SetConnectionStringName(env : string, connStr : string) : void
      {
          connectionNames[env] = connStr;
      }
      
      public ConnectionString: ConnectionStringSettings
      {
          get 
          {
              connectionStrings[ConnectionStringName];
          }
      }

      public NoDb : bool
      {
          get { !connectionStrings.ContainsKey(ConnectionStringName) }
      }
  }
}
